package com.zc.tom.service;

import com.zc.tom.common.utils.UUIDUtils;
import com.zc.tom.mapper.StipulationRecordMapper;
import com.zc.tom.pojo.QueryVo;
import com.zc.tom.pojo.StipulationRecord;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author ：WangYi
 * @date ：Created in 2019/4/22 22:38
 * @description：
 * @modified By：
 */
@Service
@Transactional
public class StipulationRecordServiceImpl implements StipulationRecordService {
    @Autowired
    private StipulationRecordMapper stipulationRecordMapper;

    // 添加学生赏罚记录信息
    @Override
    public void addStipulationRecord(StipulationRecord stipulationRecord) {
        stipulationRecord.setSrUUID(UUIDUtils.getUUID());
        stipulationRecordMapper.addStipulationRecord(stipulationRecord);
    }

    // 根据查询条件查询学生赏罚信息列表
    @Override
    public List<Map<String, Object>> queryStudentStipulationInfoByConditions(QueryVo queryVo) {
        return stipulationRecordMapper.queryStudentStipulationInfoByConditions(queryVo);
    }

    /**
     * 统计指定时间段的赏罚记录，以班级为单位
     *
     * @param params
     * @return
     */
    @Override
    public ResponseEntity<InputStreamResource> statisticsStipulationRecord(Map<String, Object> params) {
        ResponseEntity<InputStreamResource> response = null;
        try {
            List<Map<String, Object>> list = stipulationRecordMapper.statisticsStipulationRecord(params);
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet xssfSheet = workbook.createSheet();
            XSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
            XSSFRow titleRow = xssfSheet.createRow(0);
            titleRow.setRowStyle(cellStyle);
            titleRow.createCell(0).setCellValue("学生编号");
            titleRow.createCell(1).setCellValue("学生姓名");
            titleRow.createCell(2).setCellValue("赏罚名称");
            titleRow.createCell(3).setCellValue("赏罚金额");
            titleRow.createCell(4).setCellValue("时间");
            XSSFCell cell = titleRow.createCell(5);
            cell.setCellValue("备注");
            cell.setCellStyle(cellStyle);
            titleRow.setHeightInPoints(30);
            for (int i = 0; i < 5; i++) {
                xssfSheet.setColumnWidth(i, 3000);
            }
            xssfSheet.setColumnWidth(5, 6000);
            list.forEach(item -> {
                int lastRowNum = xssfSheet.getLastRowNum();
                XSSFRow dataRow = xssfSheet.createRow(lastRowNum + 1);
                dataRow.createCell(0).setCellValue(item.get("stuNo").toString());
                dataRow.createCell(1).setCellValue(item.get("stuName").toString());
                dataRow.createCell(2).setCellValue(item.get("sName").toString());
                dataRow.createCell(3).setCellValue(item.get("sMoney").toString());
                dataRow.createCell(4).setCellValue(item.get("createTime").toString());
                XSSFCell cellData = dataRow.createCell(5);
                cellData.setCellValue(item.get("remark").toString());
                cellData.setCellStyle(cellStyle);
                dataRow.setHeightInPoints(25);
            });
            String flag = null;
            if (!Boolean.parseBoolean(params.get("flag").toString()))
                flag = "违纪";
            else
                flag = "奖赏";
            String fileName = params.get("beginTime").toString() +"至"+ params.get("endTime").toString() + flag + "记录.xlsx";
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            workbook.write(out);
            workbook.close();
            list.clear();
            InputStream excelStream = new ByteArrayInputStream(out.toByteArray());
            HttpHeaders headers = new HttpHeaders();
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gbk"), "iso8859-1"));
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            response = ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(excelStream));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return response;
        }
    }

    /**
     * 统计指定时间段的赏罚记录，以个人为单位
     * @param params
     * @return
     */
    @Override
    public ResponseEntity<InputStreamResource> statisticsStipulationRecordByStuUUID(Map<String, Object> params) {
        ResponseEntity<InputStreamResource> response = null;
        try {
            List<Map<String, Object>> list = stipulationRecordMapper.statisticsStipulationRecordByStuUUID(params);
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet xssfSheet = workbook.createSheet();
            XSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
            XSSFRow titleRow = xssfSheet.createRow(0);
            titleRow.setRowStyle(cellStyle);
            titleRow.createCell(0).setCellValue("学生编号");
            titleRow.createCell(1).setCellValue("学生姓名");
            titleRow.createCell(2).setCellValue("赏罚名称");
            titleRow.createCell(3).setCellValue("赏罚金额");
            titleRow.createCell(4).setCellValue("时间");
            XSSFCell cell = titleRow.createCell(5);
            cell.setCellValue("备注");
            cell.setCellStyle(cellStyle);
            titleRow.setHeightInPoints(30);
            for (int i = 0; i < 5; i++) {
                xssfSheet.setColumnWidth(i, 3000);
            }
            xssfSheet.setColumnWidth(5, 6000);
            list.forEach(item -> {
                int lastRowNum = xssfSheet.getLastRowNum();
                XSSFRow dataRow = xssfSheet.createRow(lastRowNum + 1);
                dataRow.createCell(0).setCellValue(item.get("stuNo").toString());
                dataRow.createCell(1).setCellValue(item.get("stuName").toString());
                dataRow.createCell(2).setCellValue(item.get("sName").toString());
                dataRow.createCell(3).setCellValue(item.get("sMoney").toString());
                dataRow.createCell(4).setCellValue(item.get("createTime").toString());
                XSSFCell cellData = dataRow.createCell(5);
                cellData.setCellValue(item.get("remark").toString());
                cellData.setCellStyle(cellStyle);
                dataRow.setHeightInPoints(25);
            });
            String flag = null;
            if (!Boolean.parseBoolean(params.get("flag").toString()))
                flag = "违纪";
            else
                flag = "奖赏";
            String fileName = params.get("stuName")+params.get("beginTime").toString() +"至"+ params.get("endTime").toString() + flag + "记录.xlsx";
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            workbook.write(out);
            workbook.close();
            list.clear();
            InputStream excelStream = new ByteArrayInputStream(out.toByteArray());
            HttpHeaders headers = new HttpHeaders();
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gbk"), "iso8859-1"));
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            response = ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(excelStream));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return response;
        }
    }

}
